package com.hotchpotch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.hotchpotch.R;
import com.hotchpotch.bean.CategoryOneBean;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;

import java.util.List;

/**
 * Created by Administrator on 2017/5/17 0017.
 */

public class ClassificationLeftAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {
    private Context context;
    private List<CategoryOneBean.ResultBean> list;
    private int selectedPos = 0;
    private OnRecyclerViewItemClickListener mOnItemClickListener;

    public ClassificationLeftAdapter(Context context) {
        this.context = context;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void setList(List<CategoryOneBean.ResultBean> list) {
        this.list = list;
    }

    public void setSelectedPos(int position) {
        this.selectedPos = position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_classification_left, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).textView.setText(list.get(position).cat_alias_name);
            if (selectedPos == position) {
//                ((ViewHolder) holder).textView.setBackgroundResource(R.drawable.shape_dashed_white_solid_df514d);
                ((ViewHolder) holder).textView.setBackgroundColor(context.getResources().getColor(R.color.colorwhite));
                ((ViewHolder) holder).imageView.setVisibility(View.VISIBLE);
            } else {
//                ((ViewHolder) holder).textView.setBackgroundResource(R.drawable.shape_dashed_cfcfcf);
                ((ViewHolder) holder).textView.setBackgroundColor(context.getResources().getColor(R.color.colorcfcfcf));
                ((ViewHolder) holder).imageView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemLongClick(v);
        }
        return false;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_classification_left_text);
            imageView = (ImageView) itemView.findViewById(R.id.shu_tiao);
        }
    }
}
